﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vibz.Game.Bugs.Model;
using System.Windows.Forms;
using System.Drawing;
using Vibz.Common.Sequential;
using Vibz.Common;

namespace Vibz.Game.Bugs
{
    internal class PlayerInstance : GroundObjectInstance
    {
        public int GroundSpeed { get { return (Data.Thickness / 2) + Data.Speed; } }
        public PlayerInstance(GroundObjectInstanceList owner, Player obj)
            : base(owner, obj)
        {
            Data = obj;
        }
        public new Player Data { get; set; }
        public int GetNextLocation(Ground gnd, MoveDirection dir)
        {
            int loc = 0;
            switch (dir)
            { 
                case MoveDirection.Up:
                    loc = this.Body.Location.Y - this.GroundSpeed;
                    if (this.Body.Location.Y < 0)
                    {
                        if (this.Data.HasPower(PowerType.CrossEdge))
                            loc = gnd.Height - this.Body.Height;
                        else
                            this.Data.Reflect(ReflectAxis.X);
                    }
                    break;
                case MoveDirection.Down:
                    loc = this.Body.Location.Y + this.GroundSpeed;
                    if (this.Body.Location.Y > gnd.Height - this.Body.Height)
                    {
                        if (this.Data.HasPower(PowerType.CrossEdge))
                            loc = 0;
                        else
                            this.Data.Reflect(ReflectAxis.X);
                    }
                    break;
                case MoveDirection.Right:
                    loc = this.Body.Location.X + this.GroundSpeed;
                    if (this.Body.Location.X > gnd.Width + this.Body.Width)
                    {
                        if (this.Data.HasPower(PowerType.CrossEdge))
                            loc = 0;
                        else
                            this.Data.Reflect(ReflectAxis.Y);
                    }
                    break;
                case MoveDirection.Left:
                    loc = this.Body.Location.X - this.GroundSpeed;
                    if (this.Body.Location.X < 0)
                    {
                        if (this.Data.HasPower(PowerType.CrossEdge))
                            loc = gnd.Width - this.Body.Width;
                        else
                            this.Data.Reflect(ReflectAxis.Y);
                    }
                    break;
            }
            return loc;
        }
        public void Move(Ground gnd) 
        {
            int PlayerX = this.Body.Location.X;
            int PlayerY = this.Body.Location.Y;

            Rectangle newRect = Rectangle.Empty;
            switch (this.Data.Direction)
            {
                case MoveDirection.Up:
                    newRect = new Rectangle(new Point(PlayerX, GetNextLocation(gnd, MoveDirection.Up)), this.Body.Size);
                    break;
                case MoveDirection.UpLeft:
                    newRect = new Rectangle(new Point(GetNextLocation(gnd, MoveDirection.Left), GetNextLocation(gnd, MoveDirection.Up)), this.Body.Size);
                    break;
                case MoveDirection.UpRight:
                    newRect = new Rectangle(new Point(GetNextLocation(gnd, MoveDirection.Right), GetNextLocation(gnd, MoveDirection.Up)), this.Body.Size);
                    break;
                case MoveDirection.Down:
                    newRect = new Rectangle(new Point(PlayerX, GetNextLocation(gnd, MoveDirection.Down)), this.Body.Size);
                    break;
                case MoveDirection.DownLeft:
                    newRect = new Rectangle(new Point(GetNextLocation(gnd, MoveDirection.Left), GetNextLocation(gnd, MoveDirection.Down)), this.Body.Size);
                    break;
                case MoveDirection.DownRight:
                    newRect = new Rectangle(new Point(GetNextLocation(gnd, MoveDirection.Right), GetNextLocation(gnd, MoveDirection.Down)), this.Body.Size);
                    break;
                case MoveDirection.Left:
                    newRect = new Rectangle(new Point(GetNextLocation(gnd, MoveDirection.Left), PlayerY), this.Body.Size);
                    break;
                case MoveDirection.Right:
                    newRect = new Rectangle(new Point(GetNextLocation(gnd, MoveDirection.Right), PlayerY), this.Body.Size);
                    break;
            }
            this.Data.Location = newRect.Location;
            this.Body = newRect;

            MoveCompartment(this.Data.Next as Compartment);
        }

        void MoveCompartment(Compartment c)
        {
            if (c == null)
                return;

            try
            {
                GroundObjectInstance gi = this.Owner.Find(c);
                c.Reposition();
                gi.Body = new Rectangle(c.Location, gi.Body.Size);
                MoveCompartment(c.Next as Compartment);
            }
            catch (Exception exc)
            {
                Logs.WriteError(exc);
            }

        }
    }
}
